package com.woniuxy.microretailserver.service.impl;

import com.woniuxy.microretailserver.bean.RefundCountBean;
import com.woniuxy.microretailserver.bean.RefundCountDateBean;
import com.woniuxy.microretailserver.bean.SummaryCountDateBean;
import com.woniuxy.microretailserver.mapper.RefundCountMapper;
import com.woniuxy.microretailserver.service.IRefundCountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chenhu
 * 退款汇总业务层实现类
 */
@Transactional(readOnly = true)
@Service
public class RefundCountServiceImpl implements IRefundCountService {

    @Resource
    private RefundCountMapper refundCountMapper;
    @Override
    public List<RefundCountDateBean> findAll() {
        List<RefundCountBean> refundByTime = refundCountMapper.findRefundByTime();
        ArrayList<RefundCountDateBean> refundCountDateBeans = new ArrayList<>();
        RefundCountDateBean refundCountDateBean = new RefundCountDateBean();
        for (int i=0;i<refundByTime.size();i++){

            if (i!=0) {
                if (refundByTime.get(i).getPaymentTime().equals(refundByTime.get(i - 1).getPaymentTime())) {
                    if (refundByTime.get(i).getPayment().equals("微信")){
                        refundCountDateBean.setCountB(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumB(refundByTime.get(i).getPaymentAmountSum());
                    }else if (refundByTime.get(i).getPayment().equals("支付宝")){
                        refundCountDateBean.setCountA(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumA(refundByTime.get(i).getPaymentAmountSum());
                    }else if (refundByTime.get(i).getPayment().equals("现金")){
                        refundCountDateBean.setCountC(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumC(refundByTime.get(i).getPaymentAmountSum());
                    }
                }else{
                    refundCountDateBean.setTime(refundByTime.get(i-1).getPaymentTime());
                    refundCountDateBeans.add(refundCountDateBean);
                    refundCountDateBean=new RefundCountDateBean();
                    if (refundByTime.get(i).getPayment().equals("微信")){
                        refundCountDateBean.setCountB(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumB(refundByTime.get(i).getPaymentAmountSum());
                    }else if (refundByTime.get(i).getPayment().equals("支付宝")){
                        refundCountDateBean.setCountA(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumA(refundByTime.get(i).getPaymentAmountSum());
                    }else if (refundByTime.get(i).getPayment().equals("现金")){
                        refundCountDateBean.setCountC(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumC(refundByTime.get(i).getPaymentAmountSum());
                    }
                }
            }else {
                if (refundByTime.get(i).getPayment().equals("微信")){
                    refundCountDateBean.setCountB(refundByTime.get(i).getCount());
                    refundCountDateBean.setPaymentAmountSumB(refundByTime.get(i).getPaymentAmountSum());
                }else if (refundByTime.get(i).getPayment().equals("支付宝")){
                    refundCountDateBean.setCountA(refundByTime.get(i).getCount());
                    refundCountDateBean.setPaymentAmountSumA(refundByTime.get(i).getPaymentAmountSum());
                }else if (refundByTime.get(i).getPayment().equals("现金")){
                    refundCountDateBean.setCountC(refundByTime.get(i).getCount());
                    refundCountDateBean.setPaymentAmountSumC(refundByTime.get(i).getPaymentAmountSum());
                }
            }
        }
        refundCountDateBean.setTime(refundByTime.get(refundByTime.size()-1).getPaymentTime());
        if (refundByTime.get(refundByTime.size()-1).getPayment().equals("微信")){
            refundCountDateBean.setCountB(refundByTime.get(refundByTime.size()-1).getCount());
            refundCountDateBean.setPaymentAmountSumB(refundByTime.get(refundByTime.size()-1).getPaymentAmountSum());
        }else if (refundByTime.get(refundByTime.size()-1).getPayment().equals("支付宝")){
            refundCountDateBean.setCountA(refundByTime.get(refundByTime.size()-1).getCount());
            refundCountDateBean.setPaymentAmountSumA(refundByTime.get(refundByTime.size()-1).getPaymentAmountSum());
        }else if (refundByTime.get(refundByTime.size()-1).getPayment().equals("现金")){
            refundCountDateBean.setCountC(refundByTime.get(refundByTime.size()-1).getCount());
            refundCountDateBean.setPaymentAmountSumC(refundByTime.get(refundByTime.size()-1).getPaymentAmountSum());
        }
        refundCountDateBeans.add(refundCountDateBean);
        return refundCountDateBeans;
    }

    @Override
    public List<RefundCountDateBean> findReByTime(String time) {
        List<RefundCountBean> refundByTime = refundCountMapper.findReByTime(time);
        ArrayList<RefundCountDateBean> refundCountDateBeans = new ArrayList<>();
        RefundCountDateBean refundCountDateBean = new RefundCountDateBean();
        for (int i=0;i<refundByTime.size();i++){

            if (i!=0) {
                if (refundByTime.get(i).getPaymentTime().equals(refundByTime.get(i - 1).getPaymentTime())) {
                    if (refundByTime.get(i).getPayment().equals("微信")){
                        refundCountDateBean.setCountB(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumB(refundByTime.get(i).getPaymentAmountSum());
                    }else if (refundByTime.get(i).getPayment().equals("支付宝")){
                        refundCountDateBean.setCountA(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumA(refundByTime.get(i).getPaymentAmountSum());
                    }else if (refundByTime.get(i).getPayment().equals("现金")){
                        refundCountDateBean.setCountC(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumC(refundByTime.get(i).getPaymentAmountSum());
                    }
                }else{
                    refundCountDateBean.setTime(refundByTime.get(i-1).getPaymentTime());
                    refundCountDateBeans.add(refundCountDateBean);
                    refundCountDateBean=new RefundCountDateBean();
                    if (refundByTime.get(i).getPayment().equals("微信")){
                        refundCountDateBean.setCountB(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumB(refundByTime.get(i).getPaymentAmountSum());
                    }else if (refundByTime.get(i).getPayment().equals("支付宝")){
                        refundCountDateBean.setCountA(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumA(refundByTime.get(i).getPaymentAmountSum());
                    }else if (refundByTime.get(i).getPayment().equals("现金")){
                        refundCountDateBean.setCountC(refundByTime.get(i).getCount());
                        refundCountDateBean.setPaymentAmountSumC(refundByTime.get(i).getPaymentAmountSum());
                    }
                }
            }else {
                if (refundByTime.get(i).getPayment().equals("微信")){
                    refundCountDateBean.setCountB(refundByTime.get(i).getCount());
                    refundCountDateBean.setPaymentAmountSumB(refundByTime.get(i).getPaymentAmountSum());
                }else if (refundByTime.get(i).getPayment().equals("支付宝")){
                    refundCountDateBean.setCountA(refundByTime.get(i).getCount());
                    refundCountDateBean.setPaymentAmountSumA(refundByTime.get(i).getPaymentAmountSum());
                }else if (refundByTime.get(i).getPayment().equals("现金")){
                    refundCountDateBean.setCountC(refundByTime.get(i).getCount());
                    refundCountDateBean.setPaymentAmountSumC(refundByTime.get(i).getPaymentAmountSum());
                }
            }
        }
        refundCountDateBean.setTime(refundByTime.get(refundByTime.size()-1).getPaymentTime());
        if (refundByTime.get(refundByTime.size()-1).getPayment().equals("微信")){
            refundCountDateBean.setCountB(refundByTime.get(refundByTime.size()-1).getCount());
            refundCountDateBean.setPaymentAmountSumB(refundByTime.get(refundByTime.size()-1).getPaymentAmountSum());
        }else if (refundByTime.get(refundByTime.size()-1).getPayment().equals("支付宝")){
            refundCountDateBean.setCountA(refundByTime.get(refundByTime.size()-1).getCount());
            refundCountDateBean.setPaymentAmountSumA(refundByTime.get(refundByTime.size()-1).getPaymentAmountSum());
        }else if (refundByTime.get(refundByTime.size()-1).getPayment().equals("现金")){
            refundCountDateBean.setCountC(refundByTime.get(refundByTime.size()-1).getCount());
            refundCountDateBean.setPaymentAmountSumC(refundByTime.get(refundByTime.size()-1).getPaymentAmountSum());
        }
        refundCountDateBeans.add(refundCountDateBean);
        return refundCountDateBeans;
    }
}
